# -*- coding:utf-8 -*-
# time: 2025/7/22 ♡  16:34
# author:张伟
from flask_wtf import Form
from wtforms import StringField, IntegerField, FileField
from wtforms.validators import Email, InputRequired, Length, EqualTo, ValidationError
from flask_wtf.file import FileAllowed, FileSize
from models.auth import UserModel
from exts import cache
from flask import request


class BaseForm(Form):
    @property
    def messages(self):
        message_list = []
        if self.errors:
            for errors in self.errors.values():
                message_list.extend(errors)
        return message_list


# 注册表单
class RegisterForm(BaseForm):
    email = StringField(validators=[InputRequired(message="邮箱不能为空"), Email(message="邮箱格式错误")])
    email_captcha = StringField(
        validators=[InputRequired(message="邮箱验证码不能为空"), Length(min=6, max=6, message="请输入正确长度的验证码")])
    username = StringField(
        validators=[InputRequired(message="用户名不能为空"), Length(min=3, max=20, message="用户名长度在3-20个字符之间")])
    password = StringField(
        validators=[InputRequired(message="密码不能为空"), Length(min=6, max=20, message="密码长度在6-20个字符之间")])
    confirm_password = StringField(validators=[EqualTo("password", message="两次密码不一致")])
    graph_captcha = StringField(
        validators=[InputRequired(message="图形验证码不能为空"), Length(min=4, max=4, message="请输入正确长度的验证码")])

    def validate_email(self, field):
        email = field.data
        user = UserModel.query.filter_by(email=email).first()
        if user:
            raise ValueError("该邮箱已被注册")

    def validate_email_captcha(self, field):
        email_captcha = field.data
        email = self.email.data
        cache_captcha = cache.get(email)
        if not cache_captcha or cache_captcha.lower() != email_captcha.lower():
            raise ValueError("邮箱验证码错误")

    def validate_graph_captcha(self, field):
        graph_captcha = field.data
        key = request.cookies.get("graph_captcha_key")
        cache_captcha = cache.get(key)
        if not cache_captcha or cache_captcha.lower() != graph_captcha.lower():
            raise ValueError("图形验证码错误")


# 登录表单
class LoginForm(BaseForm):
    email = StringField(validators=[InputRequired(message="邮箱不能为空"), Email(message="邮箱格式错误")])
    password = StringField(validators=[InputRequired(message="密码不能为空")])
    remember = IntegerField()


# 上传头像表单
class UploadAvatarForm(BaseForm):
    avatar = FileField(validators=[FileAllowed(["jpg", "png", "jpeg"], message="上传的图片格式不正确！"),
                                   FileSize(1024 * 1024 * 5, message="上传的图片不能超过5M！")])


# 个性签名表单
class SignatureForm(BaseForm):
    signature = StringField(validators=[Length(min=1, max=50, message="签名长度在1-50个字符之间")])


# 上传图片表单
class UploadImageForm(BaseForm):
    image = FileField(validators=[FileAllowed(["jpg", "png", "jpeg", "gif"], message="上传的图片格式不正确！"),
                                  FileSize(1024 * 1024 * 5, message="上传的图片不能超过5M！")])


# 发布帖子表单
class PostPublicForm(BaseForm):
    title = StringField(validators=[Length(min=3, max=200, message="帖子标题必须在3-200之间！")])
    content = StringField(validators=[InputRequired(message="请传入内容！")])
    board_id = IntegerField(validators=[InputRequired(message="请传入板块ID！")])

# 修改帖子表单
class PostEditForm(BaseForm):
    title = StringField(validators=[Length(min=3, max=200, message="帖子标题必须在3-200之间！")])
    content = StringField(validators=[InputRequired(message="请传入内容！")])
    board_id = IntegerField(validators=[InputRequired(message="请传入板块ID！")])
    post_id = IntegerField(validators=[InputRequired(message="请传入帖子ID！")])


# 发布评论表单
class CommentForm(BaseForm):
    content = StringField(validators=[InputRequired(message="评论内容不能为空！")])
    post_id = IntegerField(validators=[InputRequired(message="请传入帖子ID！")])


# 点赞表单
class LikeForm(BaseForm):
    post_id = IntegerField(validators=[InputRequired(message="请传入帖子ID！")])
    user_id = StringField(validators=[InputRequired(message="请传入用户ID！")])
    like_status = IntegerField(validators=[InputRequired(message="请传入点赞状态！")])


# 修改邮箱验证码表单
class CheckEmailCaptchaForm(BaseForm):
    captcha = StringField(
        validators=[InputRequired(message="邮箱验证码不能为空"), Length(min=6, max=6, message="请输入正确的6位阿拉伯数字验证码")])


# 设置新邮箱表单
class VarifyNewEmailForm(BaseForm):
    email = StringField(validators=[InputRequired(message="新邮箱不能为空"), Email(message="邮箱格式错误")])
    captcha = StringField(
        validators=[InputRequired(message="邮箱验证码不能为空"), Length(min=6, max=6, message="请输入正确的6位阿拉伯数字验证码")])

    def validate_email(self, field):
        email = field.data
        user = UserModel.query.filter_by(email=email).first()
        if user:
            raise ValidationError("该邮箱已被注册")

    def validate_captcha(self, field):
        email = self.email.data
        captcha = field.data
        cache_captcha = cache.get(email)
        print(email, captcha, cache_captcha)
        if not cache_captcha or captcha != cache_captcha:
            raise ValidationError("邮箱验证码错误")


# 修改账号设置
class SetCountForm(BaseForm):
    username = StringField(
        validators=[InputRequired(message="用户名不能为空"), Length(min=3, max=20, message="用户名长度在3-20个字符之间")])
    signature = StringField(validators=[Length(min=1, max=50, message="签名长度在1-50个字符之间")])
